﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		
		result = structNew();
		
		courseArray = arrayNew(1);
		insArray = arrayNew(1);
		
		if ( event.isArgDefined('Keywords') AND len(event.getArg('Keywords')) ) {

			sql = "	SELECT
						c.cid,c.course_name,c.course_credit,cc.cat_name,
						i.institute_id,i.institute_name
					FROM
						t_course c INNER JOIN t_course_catalog cc ON c.cat_id = cc.cat_id
						INNER JOIN t_institute i ON i.institute_id = c.institute_id
					WHERE
						c.course_name LIKE :keyword 
					ORDER BY
						c.course_name";

			queryObj = new Query(datasource = application.dnsSlave, maxRows = 30);
			queryObj.addParam(name = "keyword", value = '%'&event.getArg('Keywords')&'%', cfsqltype = "cf_sql_varchar");
			rs_course = queryObj.execute(sql = sql).getResult();

			for( a = 1; a le rs_course.recordCount; a++ ){
				
				if ( not arrayFind(insArray, [rs_course["institute_id"][a], rs_course["institute_name"][a]]) ) {
					arrayAppend(insArray, [rs_course["institute_id"][a], rs_course["institute_name"][a]]);
				}
				
				arrayAppend(courseArray,[rs_course["cid"][a],rs_course["course_name"][a],rs_course["course_credit"][a],
										rs_course["cat_name"][a],rs_course["institute_id"][a],rs_course["institute_name"][a]]);
				
			}

		}
		
		structInsert(result, "course", courseArray);
		structInsert(result, "department", insArray);
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data: result, stringNumbers:true)#</cfoutput>